<template>
    <div class="app-container">
        <el-form ref="searchForm" inline :model="searchForm" style="position:relative;">
            <div style="text-align: right;margin-bottom: 20px;"><el-button @click="addClick" type="primary">添加字典数据</el-button></div>
        </el-form>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="ID" align="center"></el-table-column>
            <el-table-column prop="code" label="编码" align="center"></el-table-column>
            <el-table-column prop="name" label="名称" align="center"></el-table-column>
            <el-table-column prop="sort" label="顺序值" align="center"></el-table-column>
            <el-table-column prop="description" label="描述" show-overflow-tooltip align="center"></el-table-column>
            <el-table-column prop="usingStatus" :formatter="usingStatusFun" label="可用性" align="center"></el-table-column>
            <el-table-column prop="createTime" label="创建时间" align="center" width="160"></el-table-column>
            <el-table-column label="操作" width="240" align="center" fixed="right">
                <template slot-scope="scope">
                    <el-button @click.stop="editClick(scope.row)" type="primary" plain size="mini">编辑</el-button>
                    <el-button style="margin-bottom: 5px;" @click.stop="statusClick(scope.row)" type="warning" plain size="mini">{{(scope.row.usingStatus == 1) ? '禁用' : '启用'}}</el-button>
                    <el-button @click.stop="delClick(scope.row)" type="danger" plain size="mini">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @current-change="getList" layout="total, prev, pager, next, jumper" :total="pageData.totalElements"></el-pagination>
        <!-- 弹框 -->
        <el-dialog :visible.sync="showDialog" width="800px">
            <el-form ref="dataForm" :rules="rules" :model="dataForm" label-position="left" label-width="100px" style="margin: 0px 50px;">
                <el-form-item label="编码：" prop="code">
                    <el-input v-model="dataForm.code" placeholder="必填项。请输入编码"></el-input>
                </el-form-item>
                <el-form-item label="名称：" prop="name">
                    <el-input v-model="dataForm.name" placeholder="必填项。请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="顺序值：" prop="sort">
                    <el-input v-model="dataForm.sort" type="number" placeholder="请输入顺序值"></el-input>
                </el-form-item>
                <el-form-item label="描述：" prop="description">
                    <el-input type="textarea" :rows="4" v-model="dataForm.description" placeholder="请输入描述"></el-input>
                </el-form-item>
                <el-form-item label="可用性：" prop="usingStatus">
                    <el-select v-model="dataForm.usingStatus" placeholder="请选择可用性">
                        <el-option v-for="item in usingStatusOpt" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button @click="addSubmit('dataForm')" type="primary">保存</el-button>
                    <el-button @click="cancelClick('dataForm')">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
import { addRecord, findRecord, codeRecord, detailRecord, editRecord, enableRecord, delRecord } from '@/api/system/dictionaries-data'
import { getCookie, setCookie } from '@/utils/auth'
import { isvalidInitNumber } from '@/utils/validate'
export default {
    name: 'Dictionary',
    data() {
        var InitNumber=(rule, value,callback)=>{
            if(value){
                if(isvalidInitNumber(value)){
                    callback()
                }else{
                    callback(new Error('请输入整数'))
                }
            }else{
                callback()
            }
        }
        return{
            tableData: [],
            nowTit: '',
            nowId: '',
            showDialog: false,
            dataForm: {
                code: '',
                description: '',
                name: '',
                sort: '',
                usingStatus: 1,
            },
            rules: {
                code: [
                    { required: true, message: '请输入', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入', trigger: 'blur' }
                ],
                sort: [
                    { trigger: 'blur', validator: InitNumber }
                ],
            },
            searchForm:{
                
            },
            nowPage: 0,
            pageSize: 10,
            imageUrl: '',
            formData: {},
            popStatus: 0,  //0为添加，1为修改
            pageData: {},
            usingStatusOpt: [
                {label: '启用', value: 1},
                {label: '禁用', value: 0}
            ],
            applicationIdOpt: [],
        }
    },
    methods: {
        //默认函数
        init(){
            const _this = this;
            //获取列表信息
            findRecord(_this.nowPage, _this.pageSize, _this.searchForm.name, _this.searchForm.applicationId ).then(response => {
                if(response.code == 200){
                    _this.pageData = response.object;
                    _this.tableData = response.object.content;
                }else{
                    this.$message.error(response.message);
                }
            })
        },
        //添加
        addClick(){
            this.popStatus = 0;
            this.showDialog = true;
            this.dataForm.code = '',
            this.dataForm.description = '',
            this.dataForm.name = '';
            this.dataForm.sort = '';
            this.dataForm.usingStatus = 1;
            if(this.dataForm.id){
                delete this.dataForm.id;
            }
        },
        //提交添加
        addSubmit(formName){
            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //提交
                    if(_this.popStatus == 0){  //添加
                        addRecord(_this.dataForm).then(response => {
                            if(response.code == 200){
                                this.$message.success(response.message);
                                _this.init();
                                _this.showDialog = false;
                            }else{
                                this.$message.error(response.message);
                            }
                        })
                    }else if(_this.popStatus == 1){  //修改
                        _this.dataForm.id = _this.nowId;
                        editRecord(_this.dataForm).then(response => {
                            if(response.code == 200){
                                this.$message.success(response.message);
                                _this.init();
                                _this.showDialog = false;
                            }else{
                                this.$message.error(response.message);
                            }
                        })
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //删
        delClick(row){
            let _this = this;
            this.$confirm('确定删除 '+row.name+' 吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //删除
                delRecord(row.id).then(response => {
                    if(response.code == 200){
                        this.$message.success(response.message);
                        _this.init();
                    }else{
                        this.$message.error(response.message);
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });          
            });
        },
        //编辑
        editClick(row){
            this.popStatus = 1;
            this.nowId = row.id;
            this.dataForm.code = row.code;
            this.dataForm.description = row.description;
            this.dataForm.name = row.name;
            this.dataForm.sort = row.sort;
            this.dataForm.usingStatus = row.usingStatus;
            this.showDialog = true;
        },
        //取消
        cancelClick(formName){
            this.showDialog = false;
            this.$refs[formName].resetFields();
        },
        //启用禁用
        statusClick(row){
            let usingStatus,isUsing;
            if(row.usingStatus == 1){  //启用状态
                usingStatus = 0;
                isUsing = '禁用';
            }else{
                usingStatus = 1;
                isUsing = '启用';
            }

            let _this = this;
            this.$confirm('确定修改状态为 '+ isUsing +' 吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //启用禁用
                enableRecord(row.id, getCookie('pid'), usingStatus ).then(response => {
                    if(response.code == 200){
                        this.$message.success(response.message);
                        _this.init();
                    }else{
                        this.$message.error(response.message);
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });          
            });
        },
        //分页
        getList(val) {
            this.nowPage = val - 1;
            this.init();
        },
        usingStatusFun(row){
            if(row.usingStatus == 1){
                return '启用'
            }else if(row.usingStatus == 0){
                return '禁用'
            }
        },
    },
    mounted(){
        this.init();
    }
}
</script>